{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Adding YouTube Search Capability to AG2\n",
    "\n",
    "The YouTube Search integration in AG2 allows users to search for YouTube videos and retrieve video details directly within the AG2 framework. This is useful for extracting valuable information from video content, staying updated with the latest tutorials, reviews, and educational materials.\n",
    "\n",
    "## Installation\n",
    "\n",
    "To get started with the `YouTube Search` integration in AG2, follow these steps:\n",
    "\n",
    "Install AG2 with the `google-search` extra, which includes the necessary dependencies for YouTube search.\n",
    "Since our examples also use `openai`, install it as well:\n",
    "   \n",
    "```bash\n",
    "pip install -U ag2[openai,google-search]\n",
    "```\n",
    "> **Note:** If you have been using `autogen` or `ag2`, all you need to do is upgrade it using:  \n",
    "> ```bash\n",
    "> pip install -U autogen[openai,google-search]\n",
    "> ```\n",
    "> or  \n",
    "> ```bash\n",
    "> pip install -U ag2[openai,google-search]\n",
    "> ```\n",
    "> as `autogen`, and `ag2` are aliases for the same PyPI package.\n",
    "\n",
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "import autogen\n",
    "from autogen import AssistantAgent\n",
    "from autogen.tools.experimental import YoutubeSearchTool"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup YouTube Data API\n",
    "\n",
    "Before using the YouTube Search tool, you need to set up a YouTube Data API key:\n",
    "\n",
    "1. Go to the [Google Cloud Console](https://console.cloud.google.com/)\n",
    "2. Create a new project or select an existing one\n",
    "3. Navigate to **APIs & Services > Library**\n",
    "4. Search for **YouTube Data API v3** and enable it\n",
    "5. Go to **APIs & Services > Credentials**\n",
    "6. Click on **Create Credentials > API key** and copy your API key\n",
    "7. Set the API key as an environment variable:\n",
    "\n",
    "```bash\n",
    "export YOUTUBE_API_KEY=\"your_api_key\"\n",
    "```\n",
    "\n",
    "## Agent Configuration"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "config_list = autogen.config_list_from_json(\n",
    "    env_or_file=\"OAI_CONFIG_LIST\",\n",
    "    filter_dict={\n",
    "        \"model\": [\"gpt-5-nano\"],\n",
    "    },\n",
    ")\n",
    "\n",
    "assistant = AssistantAgent(\n",
    "    name=\"assistant\",\n",
    "    llm_config={\"config_list\": config_list},\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## YoutubeSearchTool Initialization\n",
    "\n",
    "Create a `YoutubeSearchTool` with your YouTube API key."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "youtube_api_key = os.getenv(\"YOUTUBE_API_KEY\")\n",
    "\n",
    "assert youtube_api_key is not None, \"Please set YOUTUBE_API_KEY environment variable\"\n",
    "\n",
    "# Create the YouTube search tool with your API key\n",
    "youtube_tool = YoutubeSearchTool(\n",
    "    youtube_api_key=youtube_api_key,\n",
    ")\n",
    "\n",
    "# Register the tool with the assistant\n",
    "youtube_tool.register_for_llm(assistant)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## YouTube Search\n",
    "\n",
    "Let's start with a basic search to find YouTube videos on a particular topic."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "run_response = assistant.run(\n",
    "    message=\"Find the latest YouTube videos on large language models. List the titles and provide brief summaries.\",\n",
    "    tools=assistant.tools,\n",
    "    max_turns=2,\n",
    "    user_input=False,\n",
    ")\n",
    "run_response.process()"
   ]
  }
 ],
 "metadata": {
  "front_matter": {
   "description": "YouTube Search Integration with AG2",
   "tags": [
    "tools",
    "youtube",
    "video",
    "search"
   ]
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
